using KamLib;

namespace Ext.Net.UIS
{

    public class ComboBox : Ext.Net.ComboBox, ITermBox
    {
        public new Context Context { get; set; }
        public Term Term { get; set; }
        public TermNode ParentTermNode { get; set; }
        public TermNode TermNode { get; set; }

        public bool ControlsCreated { get; set; }
        public void CreateControls(UisCreateControlArgs e)
        {
            this.UisGetTermNode();
            if (TermNode == null) return;

            TermNode.ApplyToField(this);
            TermNode.ApplyListeners(Listeners);
            this.UisAddTermNodeConfigItem();
            this.UisCreateLookup();
        }
    }


    public class SelectBox : Ext.Net.SelectBox, ITermBox
    {
        public new Context Context { get; set; }
        public Term Term { get; set; }
        public TermNode ParentTermNode { get; set; }
        public TermNode TermNode { get; set; }

        public bool ControlsCreated { get; set; }
        public void CreateControls(UisCreateControlArgs e)
        {
            this.UisGetTermNode();
            if (TermNode == null) return;

            TermNode.ApplyToField(this);
            TermNode.ApplyListeners(Listeners);
            this.UisAddTermNodeConfigItem();
            this.UisCreateLookup();
        }
    }


    public static class ComboBoxHelper
    {

        public static void UisCreateLookup(this ComboBoxBase cmp)
        {
            var box = cmp as ITermBox;
            if (box == null) return;

            var termNode = box.TermNode ?? cmp.UisGetTermNode();

            var term = termNode.Term;
            if (term == null) return;

            var lentityNode = termNode.LookupEntityNode;
            if (lentityNode == null) return;

            cmp.StoreID = lentityNode.StoreID;
            cmp.ValueField = term.LookupKeyFieldName;
            cmp.DisplayField = term.LookupNameFieldName;
            lentityNode.Using(term.LookupKeyField);
            lentityNode.Using(term.LookupNameField);
        }
        
    }



}